package com.staryy.activiti7_workflow;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.InputStream;
import java.util.List;

/**
 * 流程任务相关
 */
@SpringBootTest
public class Part4_Task {

    /**
     * 资源接口，包含发布信息
     */
    @Autowired
    private RepositoryService repositoryService;
    /**
     * 运行时接口，包含实例等信息
     */
    @Autowired
    private RuntimeService runtimeService;
    /**
     * 任务接口
     */
    @Autowired
    private TaskService taskService;

    /**
     * 发布流程
     */
    @Test
    public void initDeployment(){
        repositoryService.createDeployment()
                .addClasspathResource("BPMN/BaoXiao001.bpmn")
                .name("报销流程")
                .deploy();
    }

    /**
     * 启动流程
     */
    @Test
    public void startProcess(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_UEL_V2", "myProcess_UEL_V2_00123");
        System.out.println(processInstance.getProcessInstanceId());
    }

    /**
     * 查询待办
     */
    @Test
    public void getTodoByAssign(){
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("liudehua")
                .list();
        System.out.println("待办任务：");
        for(Task task : taskList){
            System.out.println("Id: "+task.getId());
            System.out.println("Name: "+task.getName());
            System.out.println("Assignee: "+task.getAssignee());
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void complateTask(){
        taskService.complete("5ef5e19b-0b1e-11ed-b12a-9cda3ebfe416");
    }

    /**
     * 认领任务
     */
    @Test
    public void cliamTask(){
        // 认领任务 (认领完后能在待办中查到)
        taskService.claim("任务ID","用户Assginee");
        // 归还任务 (认领错了，归还当前任务)
        taskService.setAssignee("任务ID","null");
        // 任务指派 (将当前任务指派给某人)
        taskService.setAssignee("任务ID","指派人Assginee");
    }
}
